import { createWebHistory, createRouter } from 'vue-router';
import { routes } from './routes';
import { ElMessage } from 'element-plus';
import { useLoginStore } from '../store/login';
import { useAsideStore } from '../store/aside';

// import {useAsideStore} from "@/store/"
export const router = createRouter({
  history: createWebHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  const flows = ["/login", "/register"];
  const sildeRoute = ["/slideDown"]
  const token = localStorage.getItem("jwtToken");
  const userRole = localStorage.getItem("userRole");
  localStorage.setItem("currentPage",to.path)
  const  haveToPageingErrors=['/taskDistribution']
  // const fromPath = ""
  //   if("")
  // 检查是否是登录或注册页面，如果是则直接放行
  if (flows.includes(to.path)) {
      next();
      return
  } else if(!sildeRoute.includes(to.path)){
    // 如果不是登录或注册页面，则检查token
    if (token==""||token==null||token==undefined) {
      // 如果没有token，则显示警告并重定向到登录页面
      ElMessage.warning("请登录后访问");
      next({ path: "/login" });
    }else{
      next();
    } 
  }else if(sildeRoute.includes(to.path)){
    next()
  }
    
});